package de.koller.worlders.engine.tiles.light;

import java.util.Iterator;

public class CircleIterator implements Iterator<CircleIterator.Point> {

	private final int radius;
	
	private int x, y;
	private State state = State.Right;
	
	public static Iterable<Point> create( final int radius ) {
		return new Iterable<Point>() {
			@Override public Iterator<Point> iterator() {
				return new CircleIterator( radius );
			}
		};
	}
	
	private CircleIterator( int radius ) {
		if( radius < 1 )
			throw new IllegalArgumentException( "radius have to be >=1." );
		
		this.radius = radius;
		x = -radius;
		y = -radius;
	}
	
	@Override
	public boolean hasNext() {
		return state != State.End;
	}

	@Override
	public Point next() {
		Point r = new Point( x, y );
		
		switch( state ) {
			case Right:
				x++;
				if( x == radius )
					state = State.Down;
				break;
			case Down:
				y++;
				if( y == radius )
					state = State.Left;
				break;
			case Left:
				x--;
				if( x == -radius )
					state = State.Up;
				break;
			case Up:
				y--;
				if( y == -radius )
					state = State.End;
				break;
		}
		
		return r;
	}
	
	@Override
	public void remove() {
		throw new UnsupportedOperationException( "Not supported yet." );
	}
	
	public static class Point {
		public int x, y;

		private Point( int x, int y ) {
			this.x = x;
			this.y = y;
		}
	}
	
	private static enum State {
		Right, Down, Left, Up, End;
	}
	
}
